/*
 * Project: Digital Wristwatch
 * Author: Zak Kemble, contact@zakkemble.co.uk
 * Copyright: (C) 2013 by Zak Kemble
 * License: GNU GPL v3 (see License.txt)
 * Web: http://blog.zakkemble.co.uk/diy-digital-wristwatch/
 */

#include <avr/pgmspace.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "typedefs.h"
#include "menus/sensor.h"
#include "display.h"
#include "menu.h"
#include "menus/functions.h"
#include "time.h"
#include "draw.h"
#include "resources.h"
#include "watchconfig.h"
#include "devices/accelerate.h"

#define OPTION_COUNT			3
#define OPTION_EXIT			OPTION_COUNT - 1


static s_prev_menu prevMenuData;

static void mDown(void);
static void mUp(void);
static void mSelect(void);
static void acc_sensor(void);
static void acc_sensor_switch(void);
static void acc_sensitivity(void);
static void acc_sensitivity_switch(void);


void mSensorOpen()
{
	setMenuInfo(OPTION_COUNT, PSTR("  < SENSOR >"), MENU_TYPE_STR, mSelect, mUp, mDown);

	acc_sensor();
	acc_sensitivity();
	setMenuOption_P(OPTION_EXIT, menuBack, NULL, back);

	setPrevMenuOpen(&prevMenuData, mSensorOpen);

	beginAnimation2(NULL);
}

static void mDown()
{
	downOption();
}

static void mUp()
{
	upOption();
}

static void mSelect()
{
	setPrevMenuExit(&prevMenuData, OPTION_EXIT);
	doAction(menuData.selected == OPTION_EXIT);
}

static void acc_sensor()
{
	const char* str;
	if(watchConfig.acceleration)
		str = PSTR("ACC Sensor ON");
	else
		str = PSTR("ACC Sensor OFF");

	setMenuOption_P(0, str, NULL, acc_sensor_switch);
}

static void acc_sensitivity()
{
	const char* str;

	if(watchConfig.sensitivity == 0)
		str = PSTR("Sensitive:Highest");
	else if(watchConfig.sensitivity == 1)
		str = PSTR("Sensitive:High");
	else if(watchConfig.sensitivity == 2)
		str = PSTR("Sensitive:Low");
	else
		str = PSTR("Sensitive:Lowest");

	setMenuOption_P(1, str, NULL, acc_sensitivity_switch);
}

static void acc_sensor_switch()
{
	watchConfig.acceleration = watchConfig.acceleration ? false : true;
	acc_sensor();
	watchconfig_save();

	if(watchConfig.acceleration) {
		accel_wake();
	} else {
		accel_sleep();
	}
}

static void acc_sensitivity_switch()
{
	byte sen = watchConfig.sensitivity;
	sen++;
	if(sen > 3)
		sen = 0;
	watchConfig.sensitivity = sen;

	acc_sensitivity();
	watchconfig_save();
}


